from django.shortcuts import render
from rest_framework.generics import GenericAPIView, ListAPIView, CreateAPIView
from rest_framework.views import APIView
from rest_framework.response import Response

from goods.models import CategoryVisitCount
from meiduo_admin.my_paginate import MyPageNumberPagination
from meiduo_admin.serializers import UserGoodsDayCountSerializer, UserSerializer
from users.models import User
from datetime import date, timedelta


# 获取用户总人数
class UserTotalCountView(APIView):
    def get(self,request):
        # 1,获取用户总人数
        # count = User.objects.filter(is_staff=False).count()
        count = User.objects.count()


        # 2.返回响应
        return Response(
            {
                "count":count
            }
        )

#  获取日增用户
class UserDayIncrementCountView(APIView):
    def get(self, request):
        # 1,获取日赠数

        today = date.today()
        count = User.objects.filter(date_joined__gte=today).count()


        # 2,返回响应
        return Response({
            'count':count
        })


# 3.获取日活数
class UserDayActiveCountView(APIView):
    def get(self,request):
        # 1,获取今天登陆过的用户
        today = date.today()
        # last_login：最近登陆的用户  gte：大于等于
        count = User.objects.filter(last_login__gte=today).count()

        # 2.返回响应
        return Response({
            'count':count
        })

# 4.获取日下单数
class UserDayOrdersCountView(APIView):
    def get(self,request):
        #1,获取日下单用户数量
        today = date.today()
        count = User.objects.filter(orderinfo__create_time__gte=today).count()


        # 2.返回响应
        return Response({
            'count':count
        })

# 5.获取月新增用户
class UserMonthIncrementCountView(APIView):
    def  get(self,request):
        # 1.获取29天前的时间
        old_date = date.today() - timedelta(day=29)

        # 2,遍历查询时间
        user_list = []
        for i in range(0,30):
            # 2.1 获取当天日期
            current_date = old_date + timedelta(days = i)

            # 2.2 获取当天日期
            next_date = old_date + timedelta(days= i+1)

            # 2.3 添加对应日期的对应新增人数
            count = User.objects.filter(date_joined__gte=current_date,date_joined__lt=next_date ).count()

            # 2,4 添加对应日期的对应人数
            user_list.append({
                'date':current_date,
                'count':count
            })


        # 3. 返回响应
        return Response(user_list)


#6,获取分类日访问量
class UserGoodsDayCountView(GenericAPIView):
    serializer_class = UserGoodsDayCountSerializer
    # queryset = CategoryVisitCount.objects.filter(date=today).all()

    def get_queryset(self):
        today = date.today()
        category_visits =  CategoryVisitCount.objects.filter(date=today).all()
        return category_visits

    def get(self,request):

        #1,获取商品分类
        # today = date.today()
        # category_visits =  CategoryVisitCount.objects.filter(date=today).all()
        category_visits = self.get_queryset()

        #2,获取序列化器
        # serializer = UserGoodsDayCountSerializer(instance=category_visits,many=True)
        serializer = self.get_serializer(instance=category_visits,many=True)


        #3,返回响应
        return Response(serializer.data)



#7,获取用户数据
class UserView(ListAPIView,CreateAPIView):
    pagination_class = MyPageNumberPagination
    serializer_class = UserSerializer
    # queryset = User.objects.all()

    def get_queryset(self):

        # 1,获取关键字
        keyword = self.request.query_params.get('keyword')

        # 2.判断关键字，返回数据源
        if keyword:
            return User.objects.filter(username__contains=keyword)

        else:
            return User.objects.all()



    # def get(self,request):
        #一, 二级视图
        #1,获取用户
        # users = User.objects.all()
        # users = self.get_queryset()
        #
        # #2,获取序列化器
        # serializer = self.get_serializer(instance=users,many=True)
        #
        # #3,返回响应
        # return Response({
        #     "lists":serializer.data,
        #     "page":1,
        #     "pages":2
        # })

        #2,二级视图 + mixin
        # return self.list(request)